package com.doublefish.doublefishrobot.ui.setting;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.video.VideoSettingMenuActivity;

/**
 * 作者:dugaofeng on 2024/10/15 17:15
 */
public class SettingFragment extends Fragment implements View.OnClickListener{
    private View view;

    private View backLL;
    private TextView titleTv;
    private TextView versionTv;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_setting, container, false);
            initViews();
        }
        return view;
    }

    private void initViews() {

        backLL = view.findViewById(R.id.ll_back);
        backLL.setVisibility(View.INVISIBLE);

        titleTv = view.findViewById(R.id.tv_title);
        titleTv.setText(R.string.setting);
        //版本
        versionTv = view.findViewById(R.id.tv_version);
        String version = getVersion(getActivity());
        versionTv.setText(version);

        TextView handTv = view.findViewById(R.id.tv_hand);
        LinearLayout versionLL = view.findViewById(R.id.ll_verson);
//        TextView userTv = view.findViewById(R.id.tv_user);
        TextView privacyTv = view.findViewById(R.id.tv_privacy);
        TextView contactTv = view.findViewById(R.id.tv_contact_us);
        TextView beianTv = view.findViewById(R.id.tv_beian);
        TextView languageTv =  view.findViewById(R.id.tv_language);
        TextView videoTv =  view.findViewById(R.id.tv_video);
        TextView safeTipsTv =  view.findViewById(R.id.tv_safe_tips);

        handTv.setOnClickListener(this);
        versionLL.setOnClickListener(this);
//        userTv.setOnClickListener(this);
        privacyTv.setOnClickListener(this);
        contactTv.setOnClickListener(this);
        beianTv.setOnClickListener(this);
        languageTv.setOnClickListener(this);
        videoTv.setOnClickListener(this);
        safeTipsTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_hand:
                toHandSwitchActivity();
                break;
            case R.id.ll_verson:
                versionCheck();
                break;
//            case R.id.tv_user: {
//                String url = "http://pc.e-doublefish.com/pingpang/user_protocol_sy.html";
//                toWebView(url, "用户协议");
//            }
//                break;
            case R.id.tv_privacy: {
                String fileName = getString(R.string.privacy_file_name);
                toPDFView(fileName, getString(R.string.privacy));
            }
                break;
            case R.id.tv_contact_us:
                toContactActivity();
                break;

            case R.id.tv_beian:
                onBeianBtnClick();
                break;

            case R.id.tv_language:
                toLanguageActivity();
                break;

            case R.id.tv_video:
                onVideoBtnClick();
                break;

            case R.id.tv_safe_tips:
                onSafeTipsBtnClick();
                break;
        }
    }

    private void toHandSwitchActivity() {
        Intent intent = new Intent(getActivity(), HandSwitchActivity.class);
        startActivity(intent);
    }

    private void versionCheck() {
        Intent intent = new Intent(getActivity(), AppVersionActivity.class);
        intent.putExtra("version", versionTv.getText().toString());
        startActivity(intent);
    }

    private void toContactActivity() {
        Intent intent = new Intent(getActivity(), ContactUsActivity.class);
        startActivity(intent);
    }

    private void toWebView(String url, String title) {
        Intent intent = WebActivity.makeIntent(getActivity(), url, title);
        startActivity(intent);
    }

    private void toPDFView(String fileName, String title) {
        Intent intent = LocalPdfReaderActivity.makeIntent(getActivity(), fileName, title);
        startActivity(intent);
    }

    private void onSafeTipsBtnClick() {
        Intent intent = new Intent(getActivity(), SafeAlertSettingActivity.class);
        startActivity(intent);
    }

    private void toLanguageActivity() {
        Intent intent = new Intent(getActivity(), LanguageActivity.class);
        startActivity(intent);
    }

    /*** 获取版本号 ***/
    private int getVersionCode(Context context) {
        //获取包管理器
        PackageManager pm = context.getPackageManager();
        //获取包信息
        try {
            PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
            //返回版本号
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

    private String getVersion(Context context) {
        //获取包管理器
        PackageManager pm = context.getPackageManager();
        //获取包信息
        try {
            PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
            //返回版本
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    private void onBeianBtnClick() {
        String url = "https://beian.miit.gov.cn";

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);
    }

    private void onBoardBtnClick() {
        Intent intent = new Intent(getActivity(), DeviceBoardInfoActivivty.class);
        startActivity(intent);
    }

    private void onVideoBtnClick() {
        Intent intent = new Intent(getActivity(), VideoSettingMenuActivity.class);
        startActivity(intent);
    }
}